package com.qf.service.impl;

import com.qf.dao.TbCourceDao;
import com.qf.dao.TbStudentDao;
import com.qf.dao.TbVariableDao;
import com.qf.dto.StudentDto;
import com.qf.pojo.*;
import com.qf.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * created by 赵文韬 on 2022/5/27 11:06
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private TbVariableDao tbVariableDao;
    @Resource
    private TbStudentDao studentDao;
    @Resource
    private TbCourceDao tbCourceDao;
    @Override
    public TbStudent selectByName(String name) {
        TbStudentExample studentExample = new TbStudentExample();
        TbStudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andUsernameEqualTo(name);
        List<TbStudent> students = studentDao.selectByExample(studentExample);
        if (students!=null && students.size()>0){
            TbStudent tbStudent = new TbStudent();
            tbStudent=students.get(0);
            return tbStudent;
        }
        return null;
    }

    @Override
    public int insertStudent(StudentDto studentDto) {
        TbStudent tbStudent = new TbStudent();
        tbStudent.setSex(studentDto.getSex());
        tbStudent.setRepassword(studentDto.getRepassword());
        tbStudent.setUsername(studentDto.getUsername());
        return studentDao.insertSelective(tbStudent);

    }


    @Override
    public TbStudent selectByNameAndPassword(String name, String password) {
        TbStudentExample studentExample = new TbStudentExample();
        TbStudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andUsernameEqualTo(name);
        criteria.andRepasswordEqualTo(password);
        List<TbStudent> tbStudents = studentDao.selectByExample(studentExample);
        return tbStudents.get(0);

    }

    @Override
    public int addCource(TbVariable tbVariable) {
        int i = tbVariableDao.insertSelective(tbVariable);
        return i;
    }

    @Override
    public int delCource(TbVariable tbVariable) {
        TbVariableExample tbVariableExample = new TbVariableExample();
        TbVariableExample.Criteria criteria = tbVariableExample.createCriteria();
        criteria.andCidEqualTo(tbVariable.getCid());
        criteria.andSidEqualTo(tbVariable.getSid());
        criteria.andIdEqualTo(tbVariable.getId());
        int i = tbVariableDao.deleteByExample(tbVariableExample);
        return i;
    }

    @Override
    public List<TbCource> selectById(int id) {
        TbVariableExample tbVariableExample = new TbVariableExample();
        TbVariableExample.Criteria criteria = tbVariableExample.createCriteria();
        criteria.andCidEqualTo(id);
        List<TbVariable> variables = tbVariableDao.selectByExample(tbVariableExample);
        ArrayList<Object> courcesId= new ArrayList<>();
        for (int i = 0; i < variables.size(); i++) {
            Integer cid = variables.get(i).getCid();
            courcesId.add(cid);
        }
        ArrayList<TbCource> cources = new ArrayList<>();
        for (int i = 0; i < courcesId.size(); i++) {
            Object o = courcesId.get(i);
            TbCource tbCource = tbCourceDao.selectByPrimaryKey((Integer) o);
            cources.add(tbCource);
        }
        return cources;
    }


}
